package com.srwl.mytx.utils;

import android.text.Editable;

public class NumberInputCheck {
    private Integer minLimit;
    private Integer maxLimit;
    private InteractionListener listener;

    public NumberInputCheck( InteractionListener listener) {
        this.listener = listener;
    }


    public NumberInputCheck(Integer minLimit, InteractionListener listener) {
        this.minLimit = minLimit;
        this.listener = listener;
    }

    public NumberInputCheck(Integer minLimit, Integer maxLimit, InteractionListener listener) {
        this.minLimit = minLimit;
        this.maxLimit = maxLimit;
        this.listener = listener;
    }

    public void onTextChanged(CharSequence charSequence) {
        if (charSequence.length() == 0) {

            listener.waitInput();

        } else if (charSequence.length() == 1 && charSequence.toString().indexOf(".") == 0) {
            listener.waitInput();
        } else {
            Integer money;
            try {
                money = Integer.valueOf(charSequence.toString());
                int posDot = charSequence.toString().indexOf(".");
                if (money == 0) {
                    //当用户输入.00时,提示输入金额错误
                    if (posDot >= 0 && charSequence.length() > 2) {
                        String[] st = charSequence.toString().split("\\.");
                        if (st.length == 2 && st[1].equals("00")) {

                            listener.onFormatError();
                        } else {
                            listener.waitInput();
                        }
                    } else {
                        if (charSequence.length() >= 9) {
                            listener.onFormatError();
                        } else {
                            listener.waitInput();
                        }
                    }
                    return;
                }
//                if (posDot < 0) {//只有在没有小数点的情况下走下面方法
//                    if (charSequence.toString().startsWith("0") && money >= 1) {
//                        if (getStartZeroNumber(charSequence.toString()) == 9) {
//                            return;
//                        }
//                    }
//                }
                if (minLimit != null && money < minLimit) {

                  //  listener.onFormatError(String.format(context.getString(R.string.input_transfer_money_limited_minimum), String.valueOf(minLimit)));
                    listener.onLessThanRange();
                    return;
                }
                if (maxLimit != null && money > maxLimit) {
                    //listener.onFormatError(String.format(context.getString(R.string.input_transfer_money_limited_max), String.valueOf(maxLimit)));
                    listener.onMoreThanRange();
                    return;
                }
                listener.inputRight();

            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
    }

    public void afterTextChanged(Editable editable) {
        String temp = editable.toString();
        int posDot = temp.indexOf(".");
        if (posDot < 0) {
            if (temp.length() - 1 > 8) {
                editable.delete(9, 10);
            }
            return;
        }
        if (temp.length() - posDot - 1 > 2) {
            editable.delete(posDot + 3, posDot + 4);
        }
    }


    public interface InteractionListener {

        void onFormatError();

        void onLessThanRange();

        void onMoreThanRange();

        void waitInput();

        void inputRight();
    }
}
